package com.uy.cuantoconocesdeuruguay.utiles;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

import com.uy.cuantoconocesdeuruguay.MainActivity;
import com.uy.cuantoconocesdeuruguay.R;

// The class has to extend the BroadcastReceiver to get the notification from the system
public class NotificationReceiver extends BroadcastReceiver {
	public static final String SUCCESS = "com.uy.cuantoconocesdeuruguay.utiles.TimeAlarm.SUCCESS";
	private static int notificationId = 0;
	@Override
	public void onReceive(Context context, Intent myIntent) {
		boolean success = myIntent.getBooleanExtra(SUCCESS, true);
		Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
		NotificationCompat.Builder mBuilder =
		        new NotificationCompat.Builder(context)
		        .setSmallIcon(R.drawable.icon)
		        .setDefaults(Notification.DEFAULT_VIBRATE)
		        .setAutoCancel(true)
		        .setSound(alarmSound);/* Add Big View Specific Configuration */
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

        // Sets a title for the Inbox style big view
        String notificationTitle;
        String line1;
        try{
			if (success){
				notificationTitle = context.getString(R.string.upload_image_result_completed_title);
				line1 = context.getString(R.string.upload_image_result_completed_image_sent);
				inboxStyle.addLine(new String(line1.getBytes(), StringPool.UTF_8));
				inboxStyle.addLine(new String(context.getString(R.string.upload_image_result_completed_image_being_analyzed).getBytes(), StringPool.UTF_8));
				inboxStyle.addLine(new String(context.getString(R.string.upload_image_result_completed_thanks).getBytes(), StringPool.UTF_8));

				mBuilder.setContentTitle(notificationTitle);
				mBuilder.setContentText(line1);
			}else{
				notificationTitle = context.getString(R.string.upload_image_result_failed_title);
				line1 = context.getString(R.string.upload_image_result_failed_image_not_sent);
				inboxStyle.addLine(new String(line1.getBytes(), StringPool.UTF_8));
				inboxStyle.addLine(new String(context.getString(R.string.upload_image_result_failed_check_connection).getBytes(), StringPool.UTF_8));
				inboxStyle.addLine(new String(context.getString(R.string.upload_image_result_failed_try_later).getBytes(), StringPool.UTF_8));

				mBuilder.setContentTitle(notificationTitle);
				mBuilder.setContentText(line1);
			}
	        inboxStyle.setBigContentTitle(notificationTitle);
	        mBuilder.setStyle(inboxStyle);
        }catch (Exception e){
        	e.printStackTrace();
        }
		Intent resultIntent = new Intent(context, MainActivity.class);
		resultIntent.putExtra(MainActivity.FROM_UPLOAD_IMAGE_NOTIFICATION, true); 
		resultIntent.putExtra(MainActivity.UPLOAD_IMAGE_SUCCESS, success); 
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
		stackBuilder.addParentStack(MainActivity.class);
		stackBuilder.addNextIntent(resultIntent);
		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
		mBuilder.setContentIntent(resultPendingIntent);
		NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		mNotificationManager.notify(++notificationId, mBuilder.build());
	}

}
